﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string wsd_Add(wsd model ,int ACID);


[OperationContract]
string wsd_Update(wsd model ,int ACID);


[OperationContract]
string wsdBypid_Delete(string pid,int ACID);


[OperationContract]
wsd wsdBypid_Get(string pid,int ACID );


[OperationContract]
List<wsd> wsd_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加wsd表数据
/// <summary>
/// 添加wsd
/// </summary>
/// <param name="model">wsd实体</param>
/// <returns>空或错误</returns>
public string wsd_Add(wsd model,int ACID)
{
if(string.IsNullOrEmpty(model.pid))
{
return "『外收单ID』不能为空！";
}
if(string.IsNullOrEmpty(model.yhmc))
{
return "『用户名称』不能为空！";
}
if(string.IsNullOrEmpty(model.jrh))
{
return "『接入号』不能为空！";
}
if(model.dlsid==null )
{
return "『代理商(交单人员)』不能为空！";
}
if(model.state==null )
{
return "『单据状态0=代理商录入 1=接收 2=录单』不能为空！";
}
if(model.Pdate ==null )
{
return "『开单日期』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.wsd.Count(p=>p.pid==model.pid)>0)
{
return "主键已存在！";
}
ctx.wsd.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加wsd表数据事物用重构
/// <summary>
/// 添加wsd事物用重构
/// </summary>
/// <param name="model">wsd实体</param>
/// <returns>空或错误</returns>
private string wsd_Add(wsd model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(string.IsNullOrEmpty(model.pid))
{
return "『外收单ID』不能为空！";
}
if(string.IsNullOrEmpty(model.yhmc))
{
return "『用户名称』不能为空！";
}
if(string.IsNullOrEmpty(model.jrh))
{
return "『接入号』不能为空！";
}
if(model.dlsid==null )
{
return "『代理商(交单人员)』不能为空！";
}
if(model.state==null )
{
return "『单据状态0=代理商录入 1=接收 2=录单』不能为空！";
}
if(model.Pdate ==null )
{
return "『开单日期』不能为空！";
}
if(ctx.wsd.Count(p=>p.pid==model.pid)>0)
{
return "主键已存在！";
}
ctx.wsd.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改wsd表数据
/// <summary>
/// 修改wsd
/// </summary>
/// <param name="model">wsd实体</param>
/// <returns>空或错误</returns>
public string wsd_Update(wsd model,int ACID)
{
if(string.IsNullOrEmpty(model.pid))
{
return "『外收单ID』不能为空！";
}
if(string.IsNullOrEmpty(model.yhmc))
{
return "『用户名称』不能为空！";
}
if(string.IsNullOrEmpty(model.jrh))
{
return "『接入号』不能为空！";
}
if(model.dlsid==null )
{
return "『代理商(交单人员)』不能为空！";
}
if(model.state==null )
{
return "『单据状态0=代理商录入 1=接收 2=录单』不能为空！";
}
if(model.Pdate ==null )
{
return "『开单日期』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.wsd.Count(p=>p.pid==model.pid)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
wsd wsdmodel=ctx.wsd.Single(p => p.pid==model.pid);
if(!string.IsNullOrEmpty(model.pid))
{
wsdmodel.pid=model.pid;
}
if(model.Pdate!=null )
{
wsdmodel.Pdate=model.Pdate;
}
if(model.dlsid!=null )
{
wsdmodel.dlsid=model.dlsid;
}
if(model.ywxzid!=null )
{
wsdmodel.ywxzid=model.ywxzid;
}
if(!string.IsNullOrEmpty(model.jrh))
{
wsdmodel.jrh=model.jrh;
}
if(!string.IsNullOrEmpty(model.yhmc))
{
wsdmodel.yhmc=model.yhmc;
}
if(!string.IsNullOrEmpty(model.yhlinktel))
{
wsdmodel.yhlinktel=model.yhlinktel;
}
if(model.tcid!=null )
{
wsdmodel.tcid=model.tcid;
}
if(!string.IsNullOrEmpty(model.yhadd))
{
wsdmodel.yhadd=model.yhadd;
}
if(!string.IsNullOrEmpty(model.yhdk))
{
wsdmodel.yhdk=model.yhdk;
}
if(model.date0!=null )
{
wsdmodel.date0=model.date0;
}
if(!string.IsNullOrEmpty(model.fsp))
{
wsdmodel.fsp=model.fsp;
}
if(model.ssje!=null )
{
wsdmodel.ssje=model.ssje;
}
if(model.jsr!=null || model.jsr.Equals(Guid.Empty))
{
wsdmodel.jsr=model.jsr;
}
if(model.jsrq!=null )
{
wsdmodel.jsrq=model.jsrq;
}
if(!string.IsNullOrEmpty(model.sdbz))
{
wsdmodel.sdbz=model.sdbz;
}
if(model.date1!=null )
{
wsdmodel.date1=model.date1;
}
if(!string.IsNullOrEmpty(model.fspff))
{
wsdmodel.fspff=model.fspff;
}
if(model.ldjfje!=null )
{
wsdmodel.ldjfje=model.ldjfje;
}
if(!string.IsNullOrEmpty(model.gdbz))
{
wsdmodel.gdbz=model.gdbz;
}
if(!string.IsNullOrEmpty(model.xcsj))
{
wsdmodel.xcsj=model.xcsj;
}
if(model.ldr!=null || model.ldr.Equals(Guid.Empty))
{
wsdmodel.ldr=model.ldr;
}
if(model.date2!=null )
{
wsdmodel.date2=model.date2;
}
if(model.state!=null )
{
wsdmodel.state=model.state;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除wsd表Bypid数据
/// <summary>
/// 删除wsdBypid
/// </summary>
/// <param name="pid">外收单ID</param>
/// <returns>空或错误</returns>
public string wsdBypid_Delete(string pid,int ACID)
{
if(string.IsNullOrEmpty(pid))
{
return "『外收单ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.wsd.Count(p=>p.pid==pid)!=1)
{
return null;
}
wsd model=ctx.wsd.Single(p => p.pid== pid);
ctx.wsd.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除wsd表Bypid数据[事物用重构]
/// <summary>
/// 删除wsdBypid[事物用重构]
/// </summary>
/// <param name="pid">外收单ID</param>
/// <returns>空或错误</returns>
private string wsdBypid_Delete(string pid,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(string.IsNullOrEmpty(pid))
{
return "『外收单ID』不能为空！";
}
if(ctx.wsd.Count(p=>p.pid==pid)!=1)
{
return null;
}
wsd model=ctx.wsd.Single(p => p.pid== pid);
ctx.wsd.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取wsd表Bypid数据
/// <summary>
/// 获取wsdBypid
/// </summary>
/// <param name="pid">外收单ID</param>
/// <returns>当前表</returns>
public wsd wsdBypid_Get(string pid,int ACID)
{
if(string.IsNullOrEmpty(pid))
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.wsd.Count(p=>p.pid==pid)!=1)
{
return null;
}
return ctx.wsd.SingleOrDefault(p=>p.pid==pid);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取wsd表数据
/// <summary>
/// 获取wsd
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<wsd></returns>
public List<wsd> wsd_Get(Expression<Func<wsd, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.wsd.ToList();
}
 else 
{
return ctx.wsd.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.wsd.Where(predicate).ToList();
}
 else 
{
return ctx.wsd.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取wsd表数据
/// <summary>
/// 获取wsd
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<wsd></returns>
public List<wsd> wsd_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return wsd_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取wsd表数据计数
/// <summary>
/// 获取wsd表数据计数
/// </summary>
/// <returns>List<wsd></returns>
public int wsd_GetCount(Expression<Func<wsd, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.wsd.Count();

}
 else
{
return ctx.wsd.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取wsd分页表数据
/// <summary>
/// 获取wsd分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<wsd></returns>
public List<wsd> wsd_Getpage(int pageIndex, int pageSize,Expression<Func<wsd, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.wsd.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.wsd.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.wsd.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.wsd.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改wsd表数据
/// <summary>
/// 批量修改wsd
/// </summary>
/// <param name="predicate">Expression<Func<wsd, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<wsd,wsd>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int wsd_batchUpdate(Expression<Func<wsd, bool>> predicate,Expression<Func<wsd, wsd>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.wsd.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改wsd表数据事物用重构
/// <summary>
/// 批量修改wsd事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<wsd, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<wsd,wsd>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int wsd_batchUpdate(Expression<Func<wsd, bool>> predicate,Expression<Func<wsd, wsd>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.wsd.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除wsd表数据
/// <summary>
/// 批量删除wsd
/// </summary>
/// <param name="predicate">Expression<Func<wsd, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int wsd_batchDel(Expression<Func<wsd, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.wsd.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除wsd表数据事物用重构
/// <summary>
/// 批量删除wsd事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<wsd, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int wsd_batchDel(Expression<Func<wsd, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.wsd.Delete(predicate);
return ok;
}
#endregion


}
}

